<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="addMenu">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
      <el-button type="danger" @click="deleteByIds">批量删除 <i class="el-icon-remove-outline"></i></el-button>
    </div>

    <el-table :data="tableData" border stripe
              :header-cell-class-name="'headerBg'"
              @selection-change="handleSelectionChange"
              row-key="id"
              default-expand-all
              >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" sortable width="180"></el-table-column>
      <el-table-column prop="name" label="名称" sortable width="180"></el-table-column>
      <el-table-column prop="path" label="路径" sortable width="180"></el-table-column>
      <el-table-column prop="pagePath" label="页面路径" sortable width="180"></el-table-column>
      <el-table-column label="图标" sortable width="180">
        <template slot-scope="scope">
          <i :class="scope.row.icon" style="font-size: 17px"></i>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述" sortable width="180"></el-table-column>
      <el-table-column label="操作"  width="300" align="center">
        <template slot-scope="scope">
          <el-button type="info" @click="handleAdd(scope.row.id)" v-if="!scope.row.pid && !scope.row.path">新增子菜单 <i class="el-icon-plus"></i></el-button>
          <el-button type="success" @click="handleUpdate(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-button type="danger" @click="deleteById(scope.row.id)">删除 <i class="el-icon-remove-outline"></i></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="新增菜单信息" :visible.sync="dialogFormVisibleAdd" width="30%">
      <el-form label-width="80px" size="small">
        <el-form-item label="名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="路径">
          <el-input v-model="form.path"></el-input>
        </el-form-item>
        <el-form-item label="页面路径">
          <el-input v-model="form.pagePath"></el-input>
        </el-form-item>
        <el-form-item label="图标">
            <el-select clearable v-model="form.icon" placeholder="请选择" style="width: 100%">
              <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.value">
                <i :class="item.value"/>{{ item.name }}
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="sureAdd">确定添加</el-button>
          <el-button @click="dialogFormVisibleAdd = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog title="修改菜单信息" :visible.sync="dialogFormVisibleUpdate" width="30%">
      <el-form label-width="80px" size="small">
        <el-form-item label="名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="路径">
          <el-input v-model="form.path"></el-input>
        </el-form-item>
        <el-form-item label="页面路径">
          <el-input v-model="form.pagePath"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-select clearable v-model="form.icon" placeholder="请选择" style="width: 100%">
            <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.value">
              <i :class="item.value"/>{{ item.name }}
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="sureUpdate">确定修改</el-button>
          <el-button @click="dialogFormVisibleUpdate = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>

import {options} from "axios";

export default {
  name: "Menu",
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      name: "",
      form: {},
      dialogFormVisibleAdd: false,
      dialogFormVisibleUpdate: false,
      collapseBtnClass: 'el-icon-s-fold',
      isCollapse: false,
      sideWidth: 200,
      logoTextShow: true,
      headerBg: 'headerBg',
      options:[],
    }
  },
  created() {
    // 请求分页查询数据
    this.load()
  },
  methods: {
    options,
    load() {
      this.request.get("/menu", {
        params: {
          name: this.name
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data

      })
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    handleSizeChange(pageSize){
      console.log(pageSize)
      this.pageNum = pageSize
      this.load()
    },
    handleAdd(pid) {
      this.dialogFormVisibleAdd = true
      this.form = {}
      if (pid){
        this.form.pid = pid
      }
      //请求图标的数据
      this.request.get("/menu/icons", {
      }).then(res => {
        this.options = res.data
      })
    },
    addMenu(){
      this.dialogFormVisibleAdd = true
      this.form = {}
    },
    handleUpdate(row) {
      this.form = row
      this.dialogFormVisibleUpdate = true
      //请求图标的数据
      this.request.get("/menu/icons", {
      }).then(res => {
        this.options = res.data
      })
    },
    sureAdd() {
      this.request.post("/menu/add", this.form).then(res => {
        if (res.data) {// 如果res为true则说明添加成功 false则说明添加失败 回传消息给前端
          this.$message.success("添加成功")
          this.dialogFormVisibleAdd = false
          this.load()
        } else {
          this.$message.error("添加失败")
        }
      })
    },
    sureUpdate() {
      this.request.post("/menu/update", this.form).then(res => {
        if (res.data) {// 如果res为true则说明修改成功 false则说明修改失败 回传消息给前端
          this.$message.success("修改成功")
          this.dialogFormVisibleUpdate = false
          this.load()
        } else {
          this.$message.error("修改失败")
        }
      })
    },
    deleteById(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.request.delete("/menu/delete/" + id)
        this.$message.success("删除成功")
        this.load()
      }).catch(() => {
        this.$message.error("已取消删除")
      });
    },
    handleSelectionChange(val){
      this.multipleSelection = val;
    },
    deleteByIds() {
      this.$confirm('此操作将永久批量删除用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
        this.request.post("/menu/delete", ids)
        this.$message.success("批量删除成功")
        this.load()
      }).catch(() => {
        this.$message.error("已取消批量删除")
      });
    },
  }
}

</script>

<style>
.headerBg {
  background: #eee!important;
}
</style>